package com.lqw.wechat.common.vo;

/**
 * Created by luqiwen on 2017/7/29.
 */
public class WeChatAccessToken {

    /**
     * // 正确获取到 access_token 时有值
     * 获取到的凭证
     */
    private String access_token;

    /**
     * // 正确获取到 access_token 时有值
     * 凭证有效时间，单位：秒
     */
    private int expires_in;

    /**
     *   // 正确获取到 access_token 时有值，存放过期时间
     */
    private long expiredTime;

    /**
     *  // 出错时有值
     *  -1	系统繁忙，此时请开发者稍候再试
     0	请求成功
     40001	AppSecret错误或者AppSecret不属于这个公众号，请开发者确认AppSecret的正确性
     40002	请确保grant_type字段值为client_credential
     40164
     调用接口的IP地址不在白名单中，请在接口IP白名单中进行设置
     */
    private int errcode;

    /**
     *   // 出错时有值
     */
    private String errmsg;

    /**
     *  accessToken 是否有效，true 有效， false 无效
     * @return
     */
    public boolean isAvailable(){
        if(expiredTime==0){
            return Boolean.FALSE;
        }
        if(errcode!=0){
            return Boolean.FALSE;
        }

        return expiredTime>System.currentTimeMillis();
    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public int getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(int expires_in) {
        this.expires_in = expires_in;
    }

    public long getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(long expiredTime) {
        if (expires_in != 0)
            this.expiredTime = System.currentTimeMillis() + ((expires_in -5) * 1000);
    }

    public int getErrcode() {
        return errcode;
    }

    public void setErrcode(int errcode) {
        this.errcode = errcode;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }
}
